package com.dianrui.qiyouriding.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dianrui.qiyouriding.Constant;
import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.adapter.CouponListAdapter;
import com.dianrui.qiyouriding.bean.CouponModel;
import com.dianrui.qiyouriding.util.JsonUtils;
import com.dianrui.qiyouriding.okhttp.OkHttpRequest;
import com.dianrui.qiyouriding.util.StringUtils;
import com.dianrui.qiyouriding.util.ToastUtil;
import com.dianrui.qiyouriding.util.Url;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class CouponListActivity extends BaseActivity {
    private List<CouponModel> couponModelList = new ArrayList<>();
    private CouponListAdapter couponListAdapter;
    private String moneys;
    private  String couponIds;
    private String isDisable;
    private String mc_id;

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.top_right)
    TextView topRight;
    @BindView(R.id.emptyView)
    LinearLayout emptyView;

    @Override
    public int getContentViewId() {
        return R.layout.coupon_list;
    }

    @Override
    public void initView() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle=getIntent().getExtras();
            if (bundle != null) {
                mc_id=bundle.getString("mc_id");
            }
        }
        customInit(true, R.color.main_color);
        title.setText(getString(R.string.recharge_txt12));
        topRight.setText(getString(R.string.sure));
        topRight.setVisibility(View.VISIBLE);
        LinearLayoutManager layoutManager = new LinearLayoutManager(CouponListActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(CouponListActivity.this, LinearLayoutManager.VERTICAL));
        couponListAdapter = new CouponListAdapter(R.layout.coupon_list_item, couponModelList);
        recyclerView.setAdapter(couponListAdapter);
        getCouponList();
        couponListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                CouponModel couponModel = couponModelList.get(position);
                switch (view.getId()){
                    case R.id.click_coupen_layout:
                        for (int i = 0; i < couponModelList.size(); i++) {
                            if (couponModelList.get(i).is_check.equals("0")) {
                                if (i == position) {
                                    couponModelList.get(i).is_check = "1";
                                    moneys=couponModel.discount;
                                    couponIds=couponModel.mc_id;
                                    isDisable=couponModel.is_disable;
                                }

                            } else {
                                couponModelList.get(i).is_check = "0";
                                moneys="";
                                couponIds="";
                                isDisable="";
                            }
                        }
                        couponListAdapter.setNewData(couponModelList);
                        break;

                    case R.id.coupon_cb_balance:
                        for (int i = 0; i < couponModelList.size(); i++) {
                            if (couponModelList.get(i).is_check.equals("0")) {
                                if (i == position) {
                                    couponModelList.get(i).is_check = "1";
                                    moneys=couponModel.discount;
                                    couponIds=couponModel.mc_id;
                                    isDisable=couponModel.is_disable;
                                }

                            } else {
                                couponModelList.get(i).is_check = "0";
                                moneys="";
                                couponIds="";
                                isDisable="";
                            }
                        }
                        couponListAdapter.setNewData(couponModelList);
                        break;

                    default:
                        break;
                }

            }
        });
    }

    @Override
    public void initData() {}



    private void getCouponList() {
        JsonObject jsonObject = new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            jsonObject.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        if (!StringUtils.isEmpty(mc_id)) {
            jsonObject.addProperty("mc_id", mc_id);
        }
        OkHttpRequest.getInstance().postRequests(Url.COUPONLIST, jsonObject.toString(),  new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        if (isFinishing()) {
                            return;
                        }
                        String status= JsonUtils.getJsonStr(result,Constant.STATUS);
                        if (Constant.DATASUCCESS.equals(status)) {
                            if (couponModelList != null) {
                                couponModelList.clear();
                            }
                            String data=JsonUtils.getJsonStr(result,"data");
                            if (!StringUtils.isEmpty(data)) {
                                List<CouponModel> list=JsonUtils.jsonStrToList(data, new TypeToken<ArrayList<CouponModel>>(){}.getType());
                                if (list != null && list.size()>0) {
                                    couponModelList.addAll(list);
                                    couponListAdapter.setNewData(couponModelList);
                                }
                            }

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (couponModelList.size() <= 0) {
                        couponListAdapter.getData().clear();
                        couponListAdapter.setEmptyView(LayoutInflater.from(CouponListActivity.this).inflate(R.layout.empty_view,null));
                        couponListAdapter.notifyDataSetChanged();
                    } else {
                        recyclerView.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }


    @OnClick({R.id.back, R.id.top_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.top_right:
                Intent intent = new Intent();
                intent.putExtra("moneys", moneys);
                intent.putExtra("coupon_id", couponIds);
                intent.putExtra("isDisable", isDisable);
                setResult(9, intent);
                finish();
                break;

                default:
                    break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (couponModelList != null) {
            couponModelList.clear();
        }
    }
}
